package com.shenhaofeng.app.widget;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


/**
 * 字符定位作用的View
 * Created by Administrator on 2015/11/16.
 */
public class LetterBar extends View {

    private List<String> letters = new ArrayList<>();

    private int MAX = 26;

    private Rect bounds = new Rect();

    private boolean hasToast = false;

    private TextView textView;

    private Paint paint;

    private onTouchLetter touchLetter;

    private onTouchLetter touchLetterDelegate = new onTouchLetter() {

        private String mLetter;


        @Override
        public void onLetter(String letter) {
            if (!TextUtils.equals(mLetter, letter)) {
                textView.setVisibility(VISIBLE);
                textView.setText(letter);
                if (touchLetter != null) {
                    touchLetter.onLetter(letter);
                }
            }
            mLetter = letter;
        }


        @Override
        public void onRelease() {
            textView.setVisibility(GONE);
            if (touchLetter != null) {
                touchLetter.onRelease();
            }
            mLetter = "";
        }
    };

    private boolean isTouching = false;

    private int touchIndex;


    public void setLetterString(List<String> letterString) {
        letters.clear();
        letters.addAll(letterString);
        MAX = letterString.size();
        invalidate();
    }


    public LetterBar(Context context) {
        super(context);
        init();
    }


    public LetterBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    public LetterBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public LetterBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }


    private void init() {
        paint = new Paint();
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setAntiAlias(true);
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        RoundRectShape shape = new RoundRectShape(new float[]{5, 5, 5, 5, 5, 5, 5, 5}, null, null);
        ShapeDrawable drawable = new ShapeDrawable(shape);
        drawable.getPaint().setColor(0x55000000);
        textView = new TextView(getContext());
        textView.setBackgroundDrawable(drawable);
        textView.setTextColor(Color.WHITE);
        textView.setGravity(Gravity.CENTER);
        int width = getResources().getDisplayMetrics().widthPixels / 4;
        int height = width / 2;
        int size = (int) (height * 2 / 3 / getResources().getDisplayMetrics().scaledDensity);
        textView.setTextSize(size);


        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(width, height);
        layoutParams.gravity = Gravity.CENTER;
        textView.setLayoutParams(layoutParams);
        textView.setVisibility(GONE);
        ViewGroup rootView = (ViewGroup) getRootView();
        rootView.addView(textView);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width;
        int height = heightMeasureSpec;
        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) {
            height = heightMeasureSpec;
            int hpx = MeasureSpec.getSize(heightMeasureSpec) / MAX;
            paint.setTextSize(hpx * 2 / 3);
        }

        width = MeasureSpec.makeMeasureSpec((int) ((paint.getTextSize() * 3 / 2)), MeasureSpec.EXACTLY);
        super.onMeasure(width, height);
        //        setMeasuredDimension(0,0);
    }


    public RectF getLetterBound(int index) {
        String letter = letters.get(index);
        paint.getTextBounds(letter, 0, letter.length(), bounds);
        float width = bounds.width();
        float height = bounds.height();
        RectF rect = new RectF();
        rect.top = getMeasuredHeight() / MAX * index;
        rect.left = (getMeasuredWidth() - width) / 2;
        rect.right = rect.left + width;
        rect.bottom = rect.top + height;
        return rect;
    }


    public int getTouchIndex(int y) {
        int sub = getHeight() / MAX;
        return y / sub;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        for (int i = 0; i < MAX; i++) {
            String letter = letters.get(i);
            RectF rectF = getLetterBound(i);
            float x = rectF.left;
            float y = rectF.top - paint.getFontMetrics().ascent + rectF.height() / 3;
            if (touchIndex == i && isTouching) {
                float size = paint.getTextSize();
                x = 0;
                int color = paint.getColor();
                paint.setColor(0xff5677fc);
                paint.setTextSize(size * 3 / 2);
                canvas.drawText(letter, x, y, paint);
                paint.setTextSize(size);
                paint.setColor(color);
            } else {
                canvas.drawText(letter, x, y, paint);
            }
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                int index = getTouchIndex((int) event.getY());
                if (index >= 0 && index < MAX && touchIndex != index) {
                    isTouching = true;
                    touchIndex = index;
                    touchLetterDelegate.onLetter(letters.get(index));
                }
                break;
            case MotionEvent.ACTION_UP: {
                isTouching = false;
                touchIndex = -1;
                touchLetterDelegate.onRelease();
                break;
            }
            default:
                if (isTouching) {
                    isTouching = false;
                    touchLetterDelegate.onRelease();
                }
                return super.onTouchEvent(event);
        }
        invalidate();
        return true;
    }


    public boolean hasToast() {
        return hasToast & getContext() instanceof Activity;
    }


    public void setToast(boolean show) {
        hasToast = show;
    }


    public void setonTouchLetter(onTouchLetter onTouchLetter) {
        this.touchLetter = onTouchLetter;
    }


    public interface onTouchLetter {

        void onLetter(String letter);


        void onRelease();


    }

}
